package de.gameduell.framework.application {
	import flash.events.TimerEvent;	
	
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.asset.SWFAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.util.Library;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.utils.Timer;	

	public class Tutorial extends Application implements ITutorial {
		public static const TUTORIAL_FINISHED:String = "tutorialFinished";
		public static const MAX_TUTORIAL_TIME:int    = 300000; 
		
		protected var skipButton:Button;
		protected var startButton:Button;
		protected var isStandaloneTutorial:Boolean;
		protected var content:MovieClip;
		protected var savedGraphicsDomain:ApplicationDomain;
		protected var savedSoundsDomain:ApplicationDomain;
		protected var maxDurationTimer:Timer;

		public function Tutorial() {
			super();	
			isStandaloneTutorial = false;
		}
		
		/**  
		 * @param aStage    = the stage of the application 
		 * @param aAssets   = comprises the assets from the configfile
		 * @param aSettings = comprises the settings from the configfile
		 * @param isOnline  = is the application online or standalone
		**/	
		override public function init(aStage:Stage, aAssets:AssetGroup, aSettings:Settings,isOnline:Boolean):void {
			super.init(aStage, aAssets, aSettings, isOnline);	
			isStandaloneTutorial = true;
			initButtons(aAssets,aSettings);
		}
		
		/**  
		 * @param aAssets   = comprises the assets from the configfile
		 * @param aSettings = comprises the settings from the configfile		 * 
		**/
		public function initButtons(aAssets:AssetGroup, aSettings:Settings):void{
			var maxTime:int = aSettings.maxTutorialTime == 0 ? MAX_TUTORIAL_TIME : aSettings.maxTutorialTime *  1000;
			maxDurationTimer = new Timer(maxTime, 1);
			maxDurationTimer.addEventListener(TimerEvent.TIMER_COMPLETE, tutorialFinished);
			library = Library.getInstance();
			try{
				if(!isStandaloneTutorial){
					var appDomain:ApplicationDomain = SWFAsset(aAssets.getAssetById("tutorial_graphics")).applicationDomain;
					savedSoundsDomain = library.soundsDomain;
					if(aAssets.assetExists("tutorial_sounds")){	
						library.soundsDomain = SWFAsset(aAssets.getAssetById("tutorial_sounds")).applicationDomain;
					}
					savedGraphicsDomain = library.graphicsDomain;
					library.graphicsDomain = appDomain;
					var tutWindow:MovieClip = library.createClip("window",appDomain);
					content = tutWindow.getChildByName("content") as MovieClip;	
					addChild(tutWindow);
				}else{
					content = MovieClip(window.getContent());
				}
				var skipButtonClip:Sprite  = content.getChildByName("skipButton") as Sprite;
				var startButtonClip:Sprite = content.getChildByName("startButton") as Sprite;

				skipButton  = new Button(skipButtonClip, "tut_skipButton");
				startButton = new Button(startButtonClip, "tut_endButton");
				
				skipButton.visible  = !isStandaloneTutorial;
				startButton.visible = !isStandaloneTutorial;
					
				skipButton.addEventListener(Button.CLICK, tutorialFinished);
				startButton.addEventListener(Button.CLICK, tutorialFinished);
				
				maxDurationTimer.start();
			}catch(e:Error){
				Debug.trace("Tutorial::initButtons: " + e.getStackTrace() + "\n" + content, Debug.ERROR);
			}
		}
		
		/**
		 * @return Sprite the Sprite of the tutorial
		 */
		public function get clip():Sprite{
			return this;
		}
		
		/**
		 * is called when the tutorial is finished
		 */
		protected function tutorialFinished(event:Event = null):void{
			maxDurationTimer.stop();
			library.graphicsDomain = savedGraphicsDomain;
			library.soundsDomain = savedSoundsDomain;
			dispatchEvent(new Event(TUTORIAL_FINISHED));
		}
	}
}
